﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using WeiSha.Core.Param.Method;

namespace WeiSha.Core.Templates
{
	// Token: 0x02000034 RID: 52
	[JsonObject(0)]
	public class TemplateConfingItem
	{
		// Token: 0x060001F7 RID: 503 RVA: 0x0000E434 File Offset: 0x0000C634
		public TemplateConfingItem(string bankName)
		{
			string text = PlatformInfoHandler.Node(Template.TemplateNodeName).Attribute("path");
			string @string = PlatformInfoHandler.Node(Template.TemplateNodeName)[bankName].Value.String;
			this._tempbankPath = new ConvertToAnyValue(text + @string);
			this._nodeItem = PlatformInfoHandler.Node(Template.TemplateNodeName)[bankName];
			string text2 = string.Empty;
			PlatformInfoHandler.SiteInfoHandlerParaNode children = this._nodeItem.GetChildren("public");
			bool flag = children != null;
			if (flag)
			{
				text2 = children.Value.String;
			}
			bool flag2 = Directory.Exists(this.Root.Physics);
			if (flag2)
			{
				DirectoryInfo[] directories = new DirectoryInfo(this.Root.Physics).GetDirectories();
				List<TemplateBank> list = new List<TemplateBank>();
				foreach (DirectoryInfo directoryInfo in directories)
				{
					bool flag3 = directoryInfo.Name.Equals("aspnet_client", StringComparison.OrdinalIgnoreCase);
					if (!flag3)
					{
						bool flag4 = directoryInfo.Name.Equals(text2, StringComparison.OrdinalIgnoreCase);
						if (!flag4)
						{
							bool flag5 = directoryInfo.Name.StartsWith("_");
							if (!flag5)
							{
								bool flag6 = directoryInfo.Name.IndexOf("_") > -1;
								if (!flag6)
								{
									list.Add(new TemplateBank(directoryInfo, this)
									{
										Path = this.GetPath(directoryInfo.Name),
										Tag = directoryInfo.Name
									});
								}
							}
						}
					}
				}
				this._items = this._orderItem(list.ToArray<TemplateBank>());
				string text3 = this._nodeItem.Attribute("default");
				foreach (TemplateBank templateBank in this.Items)
				{
					templateBank.IsCurrent = templateBank.Tag.ToLower() == text3.ToLower();
				}
			}
		}

		// Token: 0x170000BA RID: 186
		// (get) Token: 0x060001F8 RID: 504 RVA: 0x0000E658 File Offset: 0x0000C858
		public _Path Root
		{
			get
			{
				return new _Path(this._tempbankPath.VirtualPath, "");
			}
		}

		// Token: 0x170000BB RID: 187
		// (get) Token: 0x060001F9 RID: 505 RVA: 0x0000E680 File Offset: 0x0000C880
		public TemplateBank Default
		{
			get
			{
				string text = this._nodeItem.Attribute("default");
				TemplateBank templateBank = null;
				foreach (TemplateBank templateBank2 in this.Items)
				{
					bool flag = templateBank2.Tag.ToLower() == text.ToLower().Trim();
					if (flag)
					{
						templateBank = templateBank2;
						break;
					}
				}
				bool flag2 = templateBank == null;
				if (flag2)
				{
					templateBank = ((this.Items.Length != 0) ? this.Items[0] : null);
				}
				return templateBank;
			}
		}

		// Token: 0x170000BC RID: 188
		// (get) Token: 0x060001FA RID: 506 RVA: 0x0000E70C File Offset: 0x0000C90C
		public TemplateBank Public
		{
			get
			{
				bool flag = this._public == null;
				if (flag)
				{
					string text = "_public";
					DirectoryInfo directoryInfo = new DirectoryInfo(this.Root.Physics + text);
					bool flag2 = !directoryInfo.Exists;
					if (flag2)
					{
						directoryInfo.Create();
					}
					this._public = new TemplateBank(directoryInfo, this);
					this._public.Path = this.GetPath(directoryInfo.Name);
					this._public.Tag = text;
				}
				return this._public;
			}
		}

		// Token: 0x170000BD RID: 189
		// (get) Token: 0x060001FB RID: 507 RVA: 0x0000E798 File Offset: 0x0000C998
		public string Key
		{
			get
			{
				return this._nodeItem.Key;
			}
		}

		// Token: 0x170000BE RID: 190
		// (get) Token: 0x060001FC RID: 508 RVA: 0x0000E7B8 File Offset: 0x0000C9B8
		public ConvertToAnyValue Value
		{
			get
			{
				return this._nodeItem.Value;
			}
		}

		// Token: 0x170000BF RID: 191
		// (get) Token: 0x060001FD RID: 509 RVA: 0x0000E7D8 File Offset: 0x0000C9D8
		public string Tag
		{
			get
			{
				bool flag = string.IsNullOrWhiteSpace(this._tag);
				if (flag)
				{
					this._tag = this._nodeItem.Attribute("tag");
				}
				return this._tag;
			}
		}

		// Token: 0x170000C0 RID: 192
		// (get) Token: 0x060001FE RID: 510 RVA: 0x0000E818 File Offset: 0x0000CA18
		public TemplateBank Current
		{
			get
			{
				string text = this._currentName;
				bool flag = string.IsNullOrWhiteSpace(this._currentName);
				if (flag)
				{
					text = this._nodeItem.Attribute("default");
				}
				TemplateBank templateBank = null;
				foreach (TemplateBank templateBank2 in this.Items)
				{
					bool flag2 = templateBank2.Tag.ToLower() == text.ToLower().Trim();
					if (flag2)
					{
						templateBank = templateBank2;
						templateBank2.IsCurrent = true;
					}
					else
					{
						templateBank2.IsCurrent = false;
					}
				}
				bool flag3 = templateBank == null;
				if (flag3)
				{
					templateBank = this.Default;
				}
				return templateBank;
			}
		}

		// Token: 0x170000C1 RID: 193
		// (get) Token: 0x060001FF RID: 511 RVA: 0x0000E8C8 File Offset: 0x0000CAC8
		public TemplateBank[] Items
		{
			get
			{
				return this._items;
			}
		}

		// Token: 0x06000200 RID: 512 RVA: 0x0000E8E0 File Offset: 0x0000CAE0
		public _Path GetPath(string templateName)
		{
			return new _Path(this._tempbankPath.VirtualPath, templateName);
		}

		// Token: 0x06000201 RID: 513 RVA: 0x0000E904 File Offset: 0x0000CB04
		public ConvertToAnyValue GetAttr(string attr)
		{
			string text = this._nodeItem.Attribute(attr);
			return new ConvertToAnyValue(text);
		}

		// Token: 0x06000202 RID: 514 RVA: 0x0000E92C File Offset: 0x0000CB2C
		public TemplateBank SetCurrent(string tag)
		{
			this._currentName = tag;
			return this.Current;
		}

		// Token: 0x06000203 RID: 515 RVA: 0x0000E94C File Offset: 0x0000CB4C
		public TemplateBank GetTemplate(string tag)
		{
			TemplateBank templateBank = null;
			foreach (TemplateBank templateBank2 in this.Items)
			{
				bool flag = templateBank2.Tag.ToLower() == tag.ToLower().Trim();
				if (flag)
				{
					templateBank = templateBank2;
					break;
				}
			}
			bool flag2 = templateBank == null && this.Public != null;
			if (flag2)
			{
				TemplateBank @public = this.Public;
				bool flag3 = @public.Tag.ToLower() == tag.ToLower().Trim();
				if (flag3)
				{
					templateBank = @public;
				}
			}
			return templateBank;
		}

		// Token: 0x06000204 RID: 516 RVA: 0x0000E9EC File Offset: 0x0000CBEC
		public ConvertToAnyValue GetParameter(string key)
		{
			ConvertToAnyValue convertToAnyValue = null;
			PlatformInfoHandler.SiteInfoHandlerParaNode[] children = this._nodeItem.Children;
			foreach (PlatformInfoHandler.SiteInfoHandlerParaNode siteInfoHandlerParaNode in children)
			{
				bool flag = siteInfoHandlerParaNode.Key == key;
				if (flag)
				{
					convertToAnyValue = siteInfoHandlerParaNode.Value;
					break;
				}
			}
			return convertToAnyValue;
		}

		// Token: 0x06000205 RID: 517 RVA: 0x0000EA48 File Offset: 0x0000CC48
		private TemplateBank[] _orderItem(TemplateBank[] items)
		{
			for (int i = items.Length; i > 0; i--)
			{
				for (int j = 0; j < i - 1; j++)
				{
					bool flag = this.compare(items[j], items[j + 1]);
					if (flag)
					{
						TemplateBank templateBank = items[j];
						items[j] = items[j + 1];
						items[j + 1] = templateBank;
					}
				}
			}
			return items;
		}

		// Token: 0x06000206 RID: 518 RVA: 0x0000EAB4 File Offset: 0x0000CCB4
		private bool compare(TemplateBank s1, TemplateBank s2)
		{
			bool flag = string.IsNullOrWhiteSpace(s1.Name);
			bool flag2;
			if (flag)
			{
				flag2 = false;
			}
			else
			{
				bool flag3 = string.IsNullOrWhiteSpace(s2.Name);
				if (flag3)
				{
					flag2 = true;
				}
				else
				{
					bool flag4 = s1.Name.Trim() == "";
					if (flag4)
					{
						flag2 = false;
					}
					else
					{
						bool flag5 = s2.Name.Trim() == "";
						if (flag5)
						{
							flag2 = true;
						}
						else
						{
							int num = ((s1.Name.Length < s2.Name.Length) ? s1.Name.Length : s2.Name.Length);
							char[] array = s1.Name.ToCharArray();
							char[] array2 = s2.Name.ToCharArray();
							TemplateBank templateBank = null;
							for (int i = 0; i < num; i++)
							{
								bool flag6 = array[i] != array2[i];
								if (flag6)
								{
									templateBank = ((array[i] > array2[i]) ? s1 : s2);
									break;
								}
							}
							bool flag7 = templateBank == null;
							if (flag7)
							{
								templateBank = ((array.Length > array2.Length) ? s1 : s2);
							}
							flag2 = templateBank.Name.Trim() == s1.Name.Trim();
						}
					}
				}
			}
			return flag2;
		}

		// Token: 0x0400009A RID: 154
		private ConvertToAnyValue _tempbankPath;

		// Token: 0x0400009B RID: 155
		private PlatformInfoHandler.SiteInfoHandlerParaNode _nodeItem;

		// Token: 0x0400009C RID: 156
		private TemplateBank _public = null;

		// Token: 0x0400009D RID: 157
		private string _tag = string.Empty;

		// Token: 0x0400009E RID: 158
		private string _currentName = string.Empty;

		// Token: 0x0400009F RID: 159
		private TemplateBank[] _items;
	}
}
